package com.ruoyi.fly.service.impl;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.fly.domain.Feedback;
import com.ruoyi.fly.domain.vo.BatchDeleteVO;
import com.ruoyi.fly.domain.vo.FeedbackSearchVo;
import com.ruoyi.fly.domain.vo.FeedbackVo;
import com.ruoyi.fly.mapper.AppletMemberMapper;
import com.ruoyi.fly.mapper.FeedbackMapper;
import com.ruoyi.fly.service.FeedbackService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class FeedbackServiceImpl implements FeedbackService {

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private AppletMemberMapper appletMemberMapper;

    /**
     * 根据反馈id查询反馈详情
     * @param id 反馈管理主键
     * @return
     */
    @Override
    public FeedbackVo selectFeedbackById(String id) {
        FeedbackVo feedbackVo = new FeedbackVo();
        Feedback feedback = feedbackMapper.selectFeedbackById(id);
        BeanUtils.copyProperties(feedback,feedbackVo);
        feedbackVo.setNickname(appletMemberMapper.selectAppletMemberById(feedback.getMid()).getNickname());
        return feedbackVo;
    }

    /**
     * 查询反馈管理列表
     * @return
     */
    @Override
    public List<FeedbackVo> selectFeedbackList(FeedbackSearchVo feedback) {
        List<FeedbackVo> feedbackVos = feedbackMapper.selectFeedbackList(feedback);
        if (feedbackVos != null){
            feedbackVos.forEach(feedbackVo -> {
            if (feedbackVo.getMid() != null){
                feedbackVo.setNickname(appletMemberMapper.selectAppletMemberById(feedbackVo.getMid()).getNickname());
            }
            });
        }else {
            return null;
        }
        return feedbackVos;
    }

    /**
     * 新增反馈
     * @param feedback 反馈管理
     * @return
     */
    @Override
    public R insertFeedback(Feedback feedback) {
        return feedbackMapper.insertFeedback(feedback)>0?R.ok("新增成功"):R.fail("操作失败");
    }

    /**
     * 修改反馈
     * @param feedback 反馈管理
     * @return
     */
    @Override
    public R updateFeedback(Feedback feedback) {
        return feedbackMapper.updateFeedback(feedback)>0?R.ok("修改成功"):R.fail("操作失败");
    }

    /**
     * 删除反馈管理信息
     * @param id 反馈管理主键
     * @return
     */
    @Override
    public R deleteFeedbackById(String id) {
        return feedbackMapper.deleteFeedbackById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteFeedbackByIds(String[] ids) {

        return feedbackMapper.deleteFeedbackByIds(ids) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }
}
